<?php
namespace plugins\goods\controller;

use app\common\lib\file_operate\FileOperate;
use cmf\controller\PluginAdminBaseController; //引入此类
use plugins\goods\model\GoodscatsModel;
use think\Db;

/**
 * 分类控制类
 */
class GoodsCatsController extends PluginAdminBaseController
{
    //文件上传路径
    protected $upload_path = "/upload/goods/goods_classify";


    /**
     * 基础调分类选项
     */
    public function cats()
    {
        $cats     = $this->catslist();
        $pidname  = $this->pidname();
        $recently = db('goods_cats')->where('recently', 1)->find()['catid,catname'];
        $this->assign('cats', $cats);
        $this->assign('pidname', $pidname);
        $this->assign('recently', $recently);
        return $this->fetch('goods_cats/cats');
    }
    /**
     * 基础添加商品
     */
    public function goosdcats()
    {
        $param = $this->request->param();
        if (empty($param['catid'])) {
            db('goods_cats')->setField('recently', 0);
            db('goods_cats')->where('catid', $param['catid'])->setField('recently', 1);
        }
    }
    /**
     * 基础调指定分类的所有上级名称
     */
    public function pidname($catid)
    {
        $param = $this->request->param();
        $data  = db('goods_cats')->select();
        if (!empty($param['catid'])) {
            $catid = $param['catid'];
        }
        for ($i = 1; $i < 5; $i++) {
            //查询名称及上级id
            $cat     = db('goods_cats')->where('catid', $catid)->find();
            $catname = empty($catname) ? $cat['catname'] : $cat['catname'] . '  >  ' . $catname;
            //如果无上级 结束遍历
            if ($cat['pid'] == 0) {
                break;
            }
            //有上级
            $catid = $cat['pid'];
        }
        return $catname;
    }
    /**
     * 分类页
     */
    public function index()
    {
        $param = $this->request->param();
        $data  = db::name('goods_cats')->order('`catsort`,`catid` asc')->select()
                ->each(function($item){
                    if(db('goods_cats')->where('pid',$item['catid'])->find())
                    { $item['no'] = true;//有下级不显示
                    }
                    return $item;
                });
        $model = new GoodscatsModel();
        $data  = $model->catetree($data);
        $this->assign('data', $data);
        return $this->fetch();
    }
    /**
     *排序
     */
    public function listorder()
    {
        $param = $this->request->param();
        $list  = array_combine($param['id'], $param['listorder']);
        if (isset($list)) {
            foreach ($list as $id => $listorder) {
                $save = db('goods_cats')->where('catid', $id)->setField('catsort', $listorder);
            }

            $this->success('排序成功！');

            // return api_json_echo(true, '排序成功！', null, 200);
        } else {

            $this->error('排序失败！');
            // return api_json_echo(false, '排序失败！', null, 100);
        }
    }
    /**
     * 添加分类页
     */
    public function addpage()
    {
        $data = Db::name('goods_cats')->select();
        $cats = $this->catslist();
        $this->assign('data', $data);
        $this->assign('cats', $cats);
        return $this->fetch();
    }
    /**
     * 获取分类列表
     */
    public function catslist()
    {
        $cats  = db('goods_cats')->select();
        $model = new GoodscatsModel();
        $cats  = $model->catetree($cats);
        return $cats;
    }
    /**
     * 添加分类
     */
    public function add()
    {
        $param          = $this->request->param();
        $add            = $param['add'];
        $add['isshow']  = isset($add['isshow']) ? 1 : 0;
        $add['isfloor'] = isset($add['isfloor']) ? 1 : 0;
        if(empty($add['catname']))return api_json_echo(false,'标题为空！');
        $catname        = db('goods_cats')->where('catname', $add['catname'])->find();
        if ($catname) {
            return api_json_echo(false, '该分类分类已存在！', null, 100);
        }
        $return = db('goods_cats')->insert($add);
        if ($return) {
            return api_json_echo(true, '添加成功！', null, 200);
        } else {
            return api_json_echo(false, '添加失败！', null, 101);
        }
    }
    /**
     * 编辑页
     */
    public function editpage()
    {
        $param = $this->request->param();
        $data  = Db::name('goods_cats')->where('catid', $param['catid'])->find();
        $cats  = $this->catslist();
        $this->assign('data', $data);
        $this->assign('cats', $cats);
        return $this->fetch();
    }
    /**
     * 编辑
     */
    public function edit()
    {
        $param            = $this->request->post();
        $param['isshow']  = isset($param['isshow']) ? 1 : 0;
        $param['isfloor'] = isset($param['isfloor']) ? 1 : 0;
        // dump($param);exit;
        $data             = Db::name('goods_cats')->where('catid', $param['catid'])->setField($param);

        return api_json_echo(true, '编辑成功', null, 200);
    }
    /**
     * 删除多条
     */
    public function delete()
    {
        $param = $this->request->param();
        if (empty($param['catid'])) {
            $this->error('请选择删除项！');
        }
        
        $model = new GoodscatsModel;
        $catid = explode(",",$param['catid']);
        foreach($catid as $k => $v ){
            //判断是否存在下级分类
            $is_xiaji = $model->isXiaji( $v );

            if ($is_xiaji === true) {
                $this->error('请先删除下级分类');
            }
            //验证是否可删除
            $res = $model->catsGoodsNum( $v );
            if ($res['goods_count'] != 0) {
                $this->error('该分类下存在'.$res['goods_count'].'个商品，不可删除');
            }            
        }

        Db::name('goods_cats')->where('catid', 'in', $param['catid'])->delete();

        $this->success('删除成功');
    }
    /**
     * 获取分类分类名称
     */
    public function catname($catid = 0)
    {
        $data = db('goods_cats')->where('catid', $catid)->find()['catname'];
        if (empty($data)) {
            return api_json_echo(false, '分类不存在', '', 101);
        }
        return api_json_echo(true, '成功', $data, 200);
    }
    /**
     * 上传图片
     */
    public function upload()
    {
        $data        = $this->request->param();
        $plugin_name = 'goods_classify';
        $file        = $_FILES['file'];
        // $app         = $data['_plugin'];
        // $upload_path = "/plugins/" . $app . "/view/public/img/catimg/";
        $upload_path = $this->upload_path;

        $res = uploadImg($file, $upload_path);
        if ($res['status'] != true) {
            return api_json_echo(false, '上传失败', $res['message'], $res['code']);
        }
        return api_json_echo(true, '上传成功', $res['data'], 200);

        // $fo          = new FileOperate();
        // $res         = $fo->uploadImg($file, $upload_path);
        // if ($res['status'] == false) {
        //     return api_json_echo(false, '上传失败' . $res['message'], '', 200);
        // }
        // return api_json_echo(true, '上传成功', $res['data'], 200);
    }
}
